﻿using FS.Common.DotNetBean;
using FS.Common.DotNetCode;
using FS.Common.DotNetUI;
using FS.Common.DotNetData;
using FS.Business;
using System;
using System.Collections;
using System.Data;

namespace FS.Web.BLL.Visa_Base
{
    public partial class Base_SupplyCityForm2 : System.Web.UI.Page
    {
        private string _key;
        private visa_dao _visa = new visa_dao();
        protected void Page_Load(object sender, EventArgs e)
        {
            this._key = base.Request["key"];
            if (!base.IsPostBack)
            {
                initBase_CityArea();
                initBase_supply();
                initBase_City();
                if (!string.IsNullOrEmpty(this._key))
                {
                    this.InitData();
                }
            }
        }

        //加载供应商
        private void initBase_supply()
        {
            DataTable dt = _visa.GetSelect_SupplyInfo();
            ControlBindHelper.BindHtmlSelect(dt, this.Supply_id, "Supply_Name", "Supply_id", "");
        }

        //所属国家
        private void initBase_City()
        {
            DataTable dt = _visa.GetSelect_CityInfo();            
            ControlBindHelper.BindDropDownList(dt, this.City_id, "city_name", "city_id", "请选择");
        }
        //所属区域
        private void initBase_CityArea()
        {
            DataTable dt = _visa.GetSelect_CityArea();
            ControlBindHelper.BindDropDownList(dt, this.Ref_cityarea1, "property_value", "property_id", "请选择");
        }

        //签证类型
        private void initBase_Certtype(string ls_city)
        {
            DataTable dt = _visa.GetSelect_CertTypeInfoByCityID(ls_city);
            ControlBindHelper.BindDropDownList(dt, this.Ref_CertType, "name", "id", "");
        }

        private void InitData()
        {
            Hashtable ht = DataFactory.Run().GetHashtableById("Base_SupplyCity", "SupplyCity_ID", this._key);
            if (ht.Count > 0 && ht != null)
            {
                string ls_city = ht["CITY_ID"].ToString();
                string ls_certtype = ht["Ref_CertType"].ToString();
                string ls_certCityid = ht["CertCity_id"].ToString();

                this._certtype.Value = ls_certCityid + "@" + ls_certtype;
                //只显示当前城市下的签证类型，不显示所有签证类型 
                this.initBase_Certtype(ls_city);

                ControlBindHelper.SetWebControls(this.Page, ht);
            }
        }
        protected void Save_Click(object sender, EventArgs e)
        {
            Hashtable ht = new Hashtable();
            ht = ControlBindHelper.GetWebControls(this.Page);

            if (!string.IsNullOrEmpty(this._key))
            {
                ht["SupplyCity_ID"] = this._key;
                ht["ModifyDate"] = DateTime.Now;
                ht["ModifyUserId"] = RequestSession.GetSessionUser().UserId;
                ht["ModifyUserName"] = RequestSession.GetSessionUser().UserName;
            }
            else
            {
                ht["SupplyCity_ID"] = CommonHelper.GetGuid;
                ht["CreateUserId"] = RequestSession.GetSessionUser().UserId;
                ht["CreateUserName"] = RequestSession.GetSessionUser().UserName;
                ht["CreateDate"] = DateTime.Now;
            }

            string[] ls_arry = this._certtype.Value.Split('@');
            string ls_certtype = ls_arry[0];
            ht["Ref_CertType"] = ls_arry[0];
            ht["CertCity_id"] = ls_arry[1];

            string ls = _visa.Check_SupplyCity(ht["Supply_id"].ToString(), ht["City_id"].ToString(),
                 ls_certtype, ht["SupplyCity_ID"].ToString());
            if (!string.IsNullOrEmpty(ls))
            {
                ShowMsgHelper.Alert_Error(ls);
            }
            else
            {
                bool IsOk = DataFactory.Run().Submit_AddOrEdit("Base_SupplyCity", "SupplyCity_ID", this._key, ht);
                if (IsOk)
                {
                    ShowMsgHelper.Alert_Error("操作成功！");
                }
                else
                {
                    ShowMsgHelper.Alert_Error("操作失败！");
                }
            }
        }

        protected void Ref_cityarea1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ls_area = this.Ref_cityarea1.SelectedValue;
            DataTable dt = _visa.GetSelect_CityInfoByCityArea(ls_area);            
            ControlBindHelper.BindDropDownList(dt, this.City_id, "city_name", "city_id", "请选择");
        }

        protected void City_id_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ls_city = this.City_id.SelectedValue;
            //只显示当前城市下的签证类型，不显示所有签证类型 
            this.initBase_Certtype(ls_city);
        }
    }
}